/*
 * Copyright (C) 2011 Wglxy.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tigerlee.candle.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import com.tigerlee.candle.R;
import com.tigerlee.candle.util.ConnectivityInformation;

/**
 * This is a simple activity that demonstrates the dashboard user interface
 * pattern.
 * 
 */

public class HomeActivity extends Activity implements OnClickListener{
	
	View layout_feature1;
	View layout_feature2;
	View layout_feature3;
	View layout_feature4;

	private int mDefaultColor = Color.rgb(57, 142, 249);
	private int mTouchedColor = Color.rgb(137, 184, 36);
	
	private AdView mAdView;
	private String AD_UNIT_ID = "your admob publish id";
	/**
	 * onCreate - called when the activity is first created. Called when the
	 * activity is first created. This is where you should do all of your normal
	 * static set up: create views, bind data to lists, etc. This method also
	 * provides you with a Bundle containing the activity's previously frozen
	 * state, if there was one.
	 * 
	 * Always followed by onStart().
	 * 
	 */

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);
		
		if (!ConnectivityInformation
				.IsDataNetworkAvailable(getApplicationContext())) {
			if (!ConnectivityInformation
					.IsWifiAvailable(getApplicationContext())) {
				Toast.makeText(getApplicationContext(),
						getString(R.string.toast_fail_no_connection),
						Toast.LENGTH_LONG).show();
				finish();
			}
		}
		 
	}

	private void initView() {
		// TODO Auto-generated method stub
		layout_feature1 = findViewById(R.id.layout_feature1);
		layout_feature2 = findViewById(R.id.layout_feature2);
		layout_feature3 = findViewById(R.id.layout_feature3);
		layout_feature4 = findViewById(R.id.layout_feature4);

		layout_feature1.setEnabled(true);
		layout_feature2.setEnabled(true);
		layout_feature3.setEnabled(true);
		layout_feature4.setEnabled(true);
		
		layout_feature1.setBackgroundColor(mDefaultColor);
		layout_feature2.setBackgroundColor(mDefaultColor);
		layout_feature3.setBackgroundColor(mDefaultColor);
		layout_feature4.setBackgroundColor(mDefaultColor);

		ImageButton mButton1 = (ImageButton) findViewById(R.id.home_btn_feature1);
		ImageButton mButton2 = (ImageButton) findViewById(R.id.home_btn_feature2);
		ImageButton mButton3 = (ImageButton) findViewById(R.id.home_btn_feature3);
		ImageButton mButton4 = (ImageButton) findViewById(R.id.home_btn_feature4);

		mButton1.setOnClickListener(this);
		mButton2.setOnClickListener(this);
		mButton3.setOnClickListener(this);
		mButton4.setOnClickListener(this);
		// Create the adView     
        mAdView = new AdView(this, AdSize.BANNER, AD_UNIT_ID);
        
        LinearLayout mAdLayout = (LinearLayout)findViewById(R.id.ad_layout);
        // Add the adView to it
        mAdLayout.addView(mAdView);
        AdRequest MyAdRequest = new AdRequest();
        MyAdRequest.addTestDevice(MyAdRequest.TEST_EMULATOR);
        // Initiate a generic request to load it with an ad
        mAdView.loadAd(MyAdRequest);
		
	}

	/**
	 * onDestroy The final call you receive before your activity is destroyed.
	 * This can happen either because the activity is finishing (someone called
	 * finish() on it, or because the system is temporarily destroying this
	 * instance of the activity to save space. You can distinguish between these
	 * two scenarios with the isFinishing() method.
	 * 
	 */

	protected void onDestroy() {
		mAdView.destroy();
		super.onDestroy();
	}

	/**
	 * onPause Called when the system is about to start resuming a previous
	 * activity. This is typically used to commit unsaved changes to persistent
	 * data, stop animations and other things that may be consuming CPU, etc.
	 * Implementations of this method must be very quick because the next
	 * activity will not be resumed until this method returns. Followed by
	 * either onResume() if the activity returns back to the front, or onStop()
	 * if it becomes invisible to the user.
	 * 
	 */

	protected void onPause() {
		super.onPause();
	}

	/**
	 * onRestart Called after your activity has been stopped, prior to it being
	 * started again. Always followed by onStart().
	 * 
	 */

	protected void onRestart() {
		super.onRestart();
	}

	/**
	 * onResume Called when the activity will start interacting with the user.
	 * At this point your activity is at the top of the activity stack, with
	 * user input going to it. Always followed by onPause().
	 * 
	 */

	protected void onResume() {
		super.onResume();
		initView();
	}

	/**
	 * onStart Called when the activity is becoming visible to the user.
	 * Followed by onResume() if the activity comes to the foreground, or
	 * onStop() if it becomes hidden.
	 * 
	 */

	protected void onStart() {
		super.onStart();
	}

	/**
	 * onStop Called when the activity is no longer visible to the user because
	 * another activity has been resumed and is covering this one. This may
	 * happen either because a new activity is being started, an existing one is
	 * being brought in front of this one, or this one is being destroyed.
	 * 
	 * Followed by either onRestart() if this activity is coming back to
	 * interact with the user, or onDestroy() if this activity is going away.
	 */

	protected void onStop() {
		super.onStop();
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.home_btn_feature1:
			layout_feature1.setEnabled(true);
			layout_feature1.setBackgroundColor(mTouchedColor);
			startActivity(new Intent(getApplicationContext(),
					MapProtestActivity.class));
			break;
		case R.id.home_btn_feature2:
			layout_feature2.setEnabled(true);
			layout_feature2.setBackgroundColor(mTouchedColor);
			startActivity(new Intent(getApplicationContext(),
					ChatActivity.class));
			break;
		case R.id.home_btn_feature3:
			layout_feature3.setEnabled(true);
			layout_feature3.setBackgroundColor(mTouchedColor);
			startActivity(new Intent(getApplicationContext(),
					ArticleListActivity.class));
			break;
		case R.id.home_btn_feature4:
			layout_feature4.setEnabled(true);
			layout_feature4.setBackgroundColor(mTouchedColor);
			startActivity(new Intent(getApplicationContext(),
					SettingActivity.class));
			break;

		default:
			break;
		}
	}


} // end class
